package com.jy.rhin.infra.repo.notfication.message.dao;


import com.jy.rhin.infra.repo.notfication.message.EReceiveLogType;
import com.jy.rhin.infra.repo.notfication.message.model.DateRangeDto;
import com.jy.rhin.infra.repo.notfication.message.model.NormalPageQuery;
import com.jy.rhin.infra.repo.notfication.message.model.ReceiveLogDto;
import com.jy.rhin.infra.repo.notfication.message.model.ReceiveLogSummaryDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class ReceiveMessageLogQueryHandler {

    @Resource
    JdbcTemplate jdbcTemplate;


    public ReceiveLogSummaryDto querySummary(DateRangeDto query){
        String sql = "select count(id) from t_receive_message_log where #query and type=?";
        String queryStr = " 1=1 ";
        ArrayList<Object> params = new ArrayList<>();
        String time = query.getStartTime();
        if(StringUtils.isNotBlank(time)){
            try {
                Date date = DateUtils.parseDate(time + " 00:00:00", "yyyy-MM-dd hh:mm:ss");
                queryStr += "  and receive_time>=?";
                params.add(date);
            } catch (ParseException e) {
                log.error("日期解析错误", e);
            }
        }
        queryStr += "  and receive_time<=?";
        time = query.getEndTime();
        if(StringUtils.isNotBlank(time)){
            try {
                Date date = DateUtils.parseDate(time + " 23:59:59", "yyyy-MM-dd hh:mm:ss");
                params.add(date);
            } catch (ParseException e) {
                log.error("日期解析错误", e);
            }
        }else{
            params.add(new Date());
        }
        params.add(EReceiveLogType.ERROR.name());
        sql = sql.replace("#query", queryStr);
        return jdbcTemplate.query(sql, params.toArray(new Object[0]), rs -> {
            ReceiveLogSummaryDto receiveLogSummaryDto = new ReceiveLogSummaryDto();
            if(rs.next()){
                receiveLogSummaryDto.setTotalError(rs.getString(1));
            }
            return receiveLogSummaryDto;
        });
    }

    /**
     * 基于id的分页查询
     * @param query
     * @return
     */
    public List<ReceiveLogDto> queryPage(NormalPageQuery query){
        String sql = "select b.id,\n" +
                "       b.type,\n" +
                "       b.receive_time  as receiveTime,\n" +
                "       b.complete_time as completeTime,\n" +
                "       b.error_msg     as errorMsg,\n" +
                "       b.message\n" +
                "from (select id\n" +
                "      from t_receive_message_log\n" +
                "      where #query\n" +
                "    order by id desc limit #pageSize) a,\n" +
                "     t_receive_message_log b\n" +
                "where a.id = b.id order by id desc";

        String queryStr = " 1=1 ";
        ArrayList<Object> params = new ArrayList<>();
        String lastId = query.getLastId();
        if(StringUtils.isNotBlank(lastId)){
            queryStr += " and id<?";
            params.add(lastId);
        }
        String time = query.getStartTime();
        if(StringUtils.isNotBlank(time)){
            try {
                Date date = DateUtils.parseDate(time + " 00:00:00", "yyyy-MM-dd hh:mm:ss");
                queryStr += "  and receive_time>=?";
                params.add(date);
            } catch (ParseException e) {
                log.error("日期解析错误", e);
            }
        }
        queryStr += "  and receive_time<=?";
        time = query.getEndTime();
        if(StringUtils.isNotBlank(time)){
            try {
                Date date = DateUtils.parseDate(time + " 23:59:59", "yyyy-MM-dd hh:mm:ss");
                params.add(date);
            } catch (ParseException e) {
                log.error("日期解析错误", e);
            }
        }else{
            params.add(new Date());
        }

        String keyWord = query.getKeyWord();
        if(StringUtils.isNotBlank(keyWord)){
            queryStr += "  and error_msg like ?";
            params.add("%" + keyWord + "%");
        }

        sql = sql.replace("#query", queryStr).replace("#pageSize", query.getPageSize());
        return jdbcTemplate.query(sql, params.toArray(new Object[0]), rs -> {
            ArrayList<ReceiveLogDto> objects = new ArrayList<>();
            while(rs.next()){
                ReceiveLogDto dto = new ReceiveLogDto();
                dto.setId(rs.getString(1));
                dto.setType(rs.getString(2));
                dto.setReceiveTime(rs.getString(3));
                dto.setCompleteTime(rs.getString(4));
                dto.setErrorMsg(rs.getString(5));
                dto.setMessage(rs.getString(6));
                objects.add(dto);
            }
            return objects;
        });
    }
}
